package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.invoice.pojo.dto.ComputeInvoiceAmountDTO;
import com.xbongbong.pro.invoice.pojo.dto.CustomerInvoiceAndExpressGetDTO;
import com.xbongbong.pro.invoice.pojo.dto.InvoiceAmountDetailGetDTO;
import com.xbongbong.pro.invoice.pojo.dto.InvoiceGetByContractDTO;
import com.xbongbong.pro.invoice.pojo.vo.ComputeInvoiceAmountVO;
import com.xbongbong.pro.invoice.pojo.vo.CustomerInvoiceAndExpressGetVO;
import com.xbongbong.pro.invoice.pojo.vo.InvoiceAmountDetailVO;
import com.xbongbong.pro.invoice.pojo.vo.InvoiceGetByCustomerVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.service.InvoiceService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 发票
 *
 * @author feng.zheng
 * @version v1.0
 * @date 2019/2/27 17:27
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/invoice")
public class InvoiceController {

    @Resource
    private InvoiceService invoiceService;

    /**
     * 根据客户ID查询发票列表
     * @param invoiceAmountDetailGetDTO 入参
     * @param br 校验信息
     * @author zhouwq
     * @date 2019/11/20 18:48
     */
    @RequestMapping(value = "/amountDetail/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getByContract(@RequestBody @Valid InvoiceAmountDetailGetDTO invoiceAmountDetailGetDTO, BindingResult br) {
        XbbResponse<InvoiceAmountDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                //为了区分 编辑获取金额明细 或者 新建获取金额明晰
                invoiceAmountDetailGetDTO.setIsGetDetail(BasicConstant.ONE);
                InvoiceAmountDetailVO invoiceAmountDetailVO = invoiceService.getInvoiceAmountDetail(invoiceAmountDetailGetDTO);
                response = new XbbResponse<>(invoiceAmountDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取发票信息和寄送信息
     *
     * @param customerInvoiceAndExpressGetDTO 入参
     * @param br                              解析
     * @return string
     */
    @RequestMapping(value = "/customerInvoiceAndExpress/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getCustomerInvoiceAndExpress(@RequestBody @Valid CustomerInvoiceAndExpressGetDTO customerInvoiceAndExpressGetDTO, BindingResult br) {
        XbbResponse<CustomerInvoiceAndExpressGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CustomerInvoiceAndExpressGetVO customerInvoiceAndExpressGetVO = invoiceService.getCustomerInvoiceAndExpress(customerInvoiceAndExpressGetDTO);
                response = new XbbResponse<>(customerInvoiceAndExpressGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 计算发票的开票金额
     * @param computeInvoiceAmountDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/compute/invoiceAmount", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getInvoiceAmount(@RequestBody @Valid ComputeInvoiceAmountDTO computeInvoiceAmountDTO, BindingResult br) {
        XbbResponse<ComputeInvoiceAmountVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try{
                ComputeInvoiceAmountVO computeInvoiceAmountVO = invoiceService.getComputeInvoiceAmount(computeInvoiceAmountDTO);
                response = new XbbResponse<>(computeInvoiceAmountVO);
            }catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 根据客户ID查询发票列表
     * @param invoiceGetByContractDTO 入参
     * @param br 校验信息
     * @author zhouwq
     * @date 2019/11/20 18:48
     */
    @RequestMapping(value = "/list/getByContract", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getByContract(@RequestBody @Valid InvoiceGetByContractDTO invoiceGetByContractDTO, BindingResult br) {
        XbbResponse<InvoiceGetByCustomerVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try{
                TransmittableThreadLocalUtil.setValue(SaasMarkEnum.SAAS.getCode(), XbbRefTypeEnum.INVOICE.getCode(), null, invoiceGetByContractDTO.getDistributorMark());
                InvoiceGetByCustomerVO invoiceGetByCustomerVO = invoiceService.getByContract(invoiceGetByContractDTO);
                response = new XbbResponse<>(invoiceGetByCustomerVO);
            }catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
